package repositories

import (
	"gitee.com/cjf8134/bbs_serve/internal/pkg/models"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/requests"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/utils/constutil"
	"gitee.com/cjf8134/bbs_serve/internal/pkg/utils/stringutil"
	"github.com/jinzhu/gorm"
	"time"
)

// TextExists  判断是否存在
func (r *MysqlApiServeRepository) TestExist(req *requests.TextExists) (user models.User, err error) {
	model := r.db.Model(models.User{})
	switch req.Key {
	case "email":
		model = model.Where("email = ?", req.Value)
		break
	case "username":
		model = model.Where("username = ?", req.Value)
		break
	default:
		//todo
	}
	err = model.First(&user).Error
	return
}

// Register  注册
func (r *MysqlApiServeRepository) Register(req *requests.Register) (err error) {
	salt := stringutil.CreateRandomString(constutil.SaltLength)
	model := r.db.Model(models.User{})
	value := models.User{
		Username:  req.Username,
		Nickname:  req.Username,
		Password:  stringutil.CryptPassword(req.Password, salt),
		Salt:      salt,
		Email:     req.Email,
		Avatar:    "/img/default.png",
		CreatedIp: req.Ip,
		CreatedAt: time.Now(),
	}
	err = model.Create(&value).Error
	return
}

// GetUser 用户信息
func (r *MysqlApiServeRepository) GetUser(userID int) (user models.User, err error) {
	model := r.db.Model(models.User{})
	model = model.Where("id = ?", userID)
	err = model.First(&user).Error
	return
}

// GetUser 用户信息
func (r *MysqlApiServeRepository) Avatar(userID int, avatar string) (err error) {
	model := r.db.Model(models.User{})
	model = model.Where("id = ?", userID)
	err = model.UpdateColumns(map[string]interface{}{
		"avatar": avatar,
	}).Error
	return
}

// Pwd 修改密码
func (r *MysqlApiServeRepository) Pwd(req *requests.Pwd, salt string) (err error) {
	model := r.db.Model(models.User{})
	model = model.Where("id = ?", req.UserID)
	err = model.UpdateColumns(map[string]interface{}{
		"password": stringutil.CryptPassword(req.NewPwd, salt),
	}).Error
	return
}

// UserScoreNum  积分 +2
func (r *MysqlApiServeRepository) UserScoreNum(username string, num int) (err error) {
	model := r.db.Model(models.User{})
	model = model.Where("username = ?", username)
	err = model.UpdateColumns(map[string]interface{}{
		"score": gorm.Expr("score + ?", num),
	}).Error
	return
}

// UserTotal
func (r *MysqlApiServeRepository) UserTotal() (userTotal, threadTotal, CommentTotal int64) {
	r.db.Model(models.User{}).Count(&userTotal)
	r.db.Model(models.Thread{}).Count(&threadTotal)
	r.db.Model(models.Comment{}).Count(&CommentTotal)
	return
}

// ScoreSort  积分列表
func (r *MysqlApiServeRepository) ScoreSort(req *requests.ScoreSort) (userList []*models.User, err error) {
	model := r.db.Model(models.User{}).Where("score > 0")
	order := "score desc"
	err = model.Order(order).Limit(req.Limit).Find(&userList).Error
	return
}

// Email  email
func (r *MysqlApiServeRepository) Email(ems *models.Ems) (err error) {
	model := r.db.Model(models.Ems{})
	err = model.Create(&ems).Error
	return
}

// EmsVerify  email code verify
func (r *MysqlApiServeRepository) EmsVerify(event, email, code string) (ems models.Ems, err error) {
	model := r.db.Model(models.Ems{})
	model = model.Where("event = ?", event)
	model = model.Where("email = ?", email)
	model = model.Where("code = ?", code)
	err = model.Order("id desc").First(&ems).Error

	return
}

// EmsDelete  delete
func (r *MysqlApiServeRepository) EmsDelete(ID int) (err error) {
	return r.db.Where("id = ?", ID).Delete(models.Ems{}).Error
}
